package tree;

import commons.TreeNode;

import static commons.TreeNode.create;

/**
 * leet-code(101) 对称二叉树
 *
 * @author Ringo
 * @date 2021/8/29 21:42
 */
public class Symmetric {

    public static void main(String[] args) {
        TreeNode root = create(1).left(create(2).left(null).right(create(3)))
                .right(create(2).left(null).right(create(3)));

        System.out.println(createSymmetric().isSymmetric(root));

    }

    public static Symmetric createSymmetric() {
        return new Symmetric();
    }

    public boolean isSymmetric(TreeNode root) {
        return validate(root, root);
    }

    private boolean validate(TreeNode x1, TreeNode x2) {
        if (x1 == null && x2 == null)
            return true;
        if (x1 == null || x2 == null)
            return false;
        if (x1.val != x2.val)
            return false;
        boolean condition1 = validate(x1.left, x2.right);
        boolean condition2 = validate(x1.right, x2.left);
        return condition1 && condition2;
    }
}
